<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <link href="https://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.80.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>js奇淫技巧1 &middot; K J</title>
  <meta name="description" content="" />

  
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/poole.css">
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="icon" href="/hugo/favicon.png">
  
  
</head>

  <body class="theme-base-09 ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="http://kartjim.gitee.io/hugo/"><h1>K J</h1></a>
      <p class="lead">
       kart jim`blog 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="http://kartjim.gitee.io/hugo/">Home</a> </li>
        <li><a href="https://blog.csdn.net/qq_46590483?spm=1001.2100.3001.5343"> CSDN </a></li><li><a href="https://github.com/can-dy-jack"> Github </a></li><li><a href="https://www.cnblogs.com/kartjim"> 博客园 </a></li>
      </ul>
    </nav>

    <p>Copyright 2021 © kart jim</p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>js奇淫技巧1</h1>
  <time datetime=2021-01-02T16:30:37Z class="post-date">Sat, Jan 2, 2021</time>
  <h2 id="函数">函数</h2>
<h3 id="变量作用域">变量作用域</h3>
<ul>
<li>var</li>
</ul>
<p>JavaScript有两种作用域：全局 / 局部作用域</p>
<blockquote>
<p>var声明的变量 在函数内部声明的变量只存在于函数内部</p>
</blockquote>
<blockquote>
<p>用 this 可以调用全局作用域</p>
</blockquote>
<ul>
<li>let</li>
</ul>
<p>为了实现变量块级作用域，js用一个<code>let</code>关键字声明具有严格块级作用域的变量。</p>
<ul>
<li>const</li>
</ul>
<p>const关键字用于声明一个常量，使常量不能被重新赋值或重新声明。</p>
<h3 id="箭头函数">箭头函数</h3>
<p>先看一般的匿名函数：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">sayHello</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">function</span>(){ <span style="color:#a6e22e">alert</span>(<span style="color:#e6db74">&#34;Hello&#34;</span>);};
</code></pre></div><p>箭头函数（arrow function）可以使匿名函数更加简洁：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">sayHello</span> <span style="color:#f92672">=</span> () =&gt; <span style="color:#a6e22e">alert</span>(<span style="color:#e6db74">&#39;Hello&#39;</span>);
</code></pre></div><blockquote>
<p>只有包含多个表达式，才需要花括号。</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#a6e22e">myFunc</span> <span style="color:#f92672">=</span> (<span style="color:#a6e22e">x</span>,<span style="color:#a6e22e">y</span>) =&gt; {
     <span style="color:#a6e22e">x</span> <span style="color:#f92672">+=</span> <span style="color:#a6e22e">y</span>;
     <span style="color:#a6e22e">alert</span>(<span style="color:#a6e22e">x</span>); 
     }
</code></pre></div><blockquote>
<p>=&gt;符号通常被称为胖箭头符号</p>
</blockquote>
<h3 id="默认参数">默认参数</h3>
<p>要指定一个默认参数，只要给函数定义中的参数一个默认值就行。
例如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">warn</span>(<span style="color:#a6e22e">temp</span>,<span style="color:#a6e22e">subtitle</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;--warning&#39;</span>){
    <span style="color:#a6e22e">alert</span>(<span style="color:#a6e22e">subtitle</span><span style="color:#f92672">+</span><span style="color:#e6db74">&#34; , &#34;</span><span style="color:#f92672">+</span><span style="color:#a6e22e">temp</span><span style="color:#f92672">+</span><span style="color:#e6db74">&#34; ;&#34;</span>);
}
</code></pre></div><p><strong>在函数定义中，一般将有默认值的参数，放在没有默认值的参数后面</strong></p>
</div>


    </main>

    
      
    
  </body>
</html>
